

<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="./">
<head>
  <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Advanced Installation &mdash; CodeCarbon 3.1.1 documentation</title>
      <link rel="stylesheet" type="text/css" href="_static/pygments.css?v=03e43079" />
      <link rel="stylesheet" type="text/css" href="_static/css/theme.css?v=e59714d7" />

  
      <script src="_static/jquery.js?v=5d32c60e"></script>
      <script src="_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
      <script src="_static/documentation_options.js?v=796a81b5"></script>
      <script src="_static/doctools.js?v=9bcbadda"></script>
      <script src="_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="_static/js/theme.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Output" href="output.html" />
    <link rel="prev" title="Comet Integration" href="comet.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="index.html" class="icon icon-home">
            CodeCarbon
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption" role="heading"><span class="caption-text">Introduction</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="motivation.html">Motivation</a></li>
<li class="toctree-l1"><a class="reference internal" href="methodology.html">Methodology</a></li>
<li class="toctree-l1"><a class="reference internal" href="rapl.html">RAPL Metrics</a></li>
<li class="toctree-l1"><a class="reference internal" href="model_examples.html">Model Comparisons</a></li>
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently Asked Questions</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Getting Started</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="installation.html">Installing CodeCarbon</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage.html">Quickstart</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage.html#configuration">Configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">CodeCarbon API</a></li>
<li class="toctree-l1"><a class="reference internal" href="parameters.html">Parameters</a></li>
<li class="toctree-l1"><a class="reference internal" href="examples.html">Examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="comet.html">Comet Integration</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Advanced Installation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#install-codecarbon-as-a-linux-service">Install CodeCarbon as a Linux service</a></li>
<li class="toctree-l2"><a class="reference internal" href="#deploy-codecarbon-cli-as-a-service-using-ansible">Deploy CodeCarbon CLI as a Service using Ansible</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#what-the-playbook-does">What the Playbook Does</a></li>
<li class="toctree-l3"><a class="reference internal" href="#prerequisites">Prerequisites</a></li>
<li class="toctree-l3"><a class="reference internal" href="#directory-structure">Directory Structure</a></li>
<li class="toctree-l3"><a class="reference internal" href="#quick-start">Quick Start</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Logging</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="output.html">Output</a></li>
<li class="toctree-l1"><a class="reference internal" href="to_logger.html">Collecting emissions to a logger</a></li>
<li class="toctree-l1"><a class="reference internal" href="visualize.html">Visualize</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="index.html">CodeCarbon</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="index.html" class="icon icon-home" aria-label="Home"></a></li>
      <li class="breadcrumb-item active">Advanced Installation</li>
      <li class="wy-breadcrumbs-aside">
            <a href="_sources/advanced_installation.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="advanced-installation">
<h1>Advanced Installation<a class="headerlink" href="#advanced-installation" title="Link to this heading"></a></h1>
<section id="install-codecarbon-as-a-linux-service">
<h2>Install CodeCarbon as a Linux service<a class="headerlink" href="#install-codecarbon-as-a-linux-service" title="Link to this heading"></a></h2>
<p>To install CodeCarbon as a Linux service, follow the instructions below. It works on Ubuntu or other Debian-based systems using systemd.</p>
<p>Create a dedicated user:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>sudo<span class="w"> </span>useradd<span class="w"> </span>-r<span class="w"> </span>-s<span class="w"> </span>/bin/false<span class="w"> </span>codecarbon
</pre></div>
</div>
<p>Create a directory for the CodeCarbon service:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>sudo<span class="w"> </span>mkdir<span class="w"> </span>/opt/codecarbon
</pre></div>
</div>
<p>Change the ownership of the directory to the user created above:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>sudo<span class="w"> </span>chown<span class="w"> </span>codecarbon:codecarbon<span class="w"> </span>/opt/codecarbon
</pre></div>
</div>
<p>Create a virtual environment for CodeCarbon :</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>sudo<span class="w"> </span>apt<span class="w"> </span>install<span class="w"> </span>python3-venv
sudo<span class="w"> </span>-u<span class="w"> </span>codecarbon<span class="w"> </span>python3<span class="w"> </span>-m<span class="w"> </span>venv<span class="w"> </span>/opt/codecarbon/.venv
</pre></div>
</div>
<p>Install CodeCarbon in the virtual environment:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>sudo<span class="w"> </span>-u<span class="w"> </span>codecarbon<span class="w"> </span>/opt/codecarbon/.venv/bin/pip<span class="w"> </span>install<span class="w"> </span>codecarbon
</pre></div>
</div>
<p>Go to <a class="reference external" href="https://dashboard.codecarbon.io/">https://dashboard.codecarbon.io/</a> and create an account to get your API key.</p>
<p>Configure CodeCarbon:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>sudo<span class="w"> </span>-u<span class="w"> </span>codecarbon<span class="w"> </span>/opt/codecarbon/.venv/bin/codecarbon<span class="w"> </span>login
</pre></div>
</div>
<p>Create a systemd service file:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>sudo<span class="w"> </span>tee<span class="w"> </span>/etc/systemd/system/codecarbon.service<span class="w"> </span><span class="s">&lt;&lt;EOF</span>
<span class="s">[Unit]</span>
<span class="s">Description=CodeCarbon service</span>
<span class="s">After=network.target</span>

<span class="s">[Service]</span>
<span class="s">User=codecarbon</span>
<span class="s">Group=codecarbon</span>
<span class="s">WorkingDirectory=/opt/codecarbon</span>
<span class="s">ExecStart=/opt/codecarbon/.venv/bin/codecarbon monitor</span>
<span class="s">Restart=always</span>

<span class="s">[Install]</span>
<span class="s">WantedBy=multi-user.target</span>
<span class="s">EOF</span>
</pre></div>
</div>
<p>Give permissions to the <code class="docutils literal notranslate"><span class="pre">codecarbon</span></code> group to read the RAPL (Running Average Power Limit) information:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>sudo<span class="w"> </span>chown<span class="w"> </span>-R<span class="w"> </span>root:codecarbon<span class="w"> </span>/sys/class/powercap/intel-rapl/*
sudo<span class="w"> </span>chmod<span class="w"> </span>g+r<span class="w"> </span>-R<span class="w"> </span>/sys/class/powercap/intel-rapl/*

sudo<span class="w"> </span>apt<span class="w"> </span>install<span class="w"> </span>sysfsutils
<span class="nb">echo</span><span class="w"> </span><span class="s2">&quot;mode class/powercap/intel-rapl:0/energy_uj = 0440&quot;</span><span class="w"> </span>&gt;&gt;<span class="w"> </span>/etc/sysfs.conf
<span class="nb">echo</span><span class="w"> </span><span class="s2">&quot;owner class/powercap/intel-rapl:0/energy_uj = root:codecarbon&quot;</span><span class="w"> </span>&gt;&gt;<span class="w"> </span>/etc/sysfs.conf
</pre></div>
</div>
<p>Create the configuration file for CodeCarbon:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>sudo<span class="w"> </span>tee<span class="w"> </span>/opt/codecarbon/.codecarbon.config<span class="w"> </span><span class="s">&lt;&lt;EOF</span>
<span class="s">[codecarbon]</span>
<span class="s">api_endpoint = https://api.codecarbon.io</span>
<span class="s">organization_id = &lt;organization_id&gt;</span>
<span class="s">project_id = &lt;project_id&gt;</span>
<span class="s">experiment_id = &lt;experiment_id&gt;</span>
<span class="s">api_key = &lt;api_key&gt;</span>
<span class="s"># Verbose logging</span>
<span class="s">log_level=WARNING</span>
<span class="s"># Measure power every 30 seconds</span>
<span class="s">measure_power_secs=30</span>
<span class="s"># Send measure to API every 5 minutes (10*30 seconds)</span>
<span class="s">api_call_interval=10</span>
<span class="s">EOF</span>
</pre></div>
</div>
<p>Enable and start the service:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>sudo<span class="w"> </span>systemctl<span class="w"> </span><span class="nb">enable</span><span class="w"> </span>codecarbon
sudo<span class="w"> </span>systemctl<span class="w"> </span>start<span class="w"> </span>codecarbon
</pre></div>
</div>
<p>Check the traces of the service:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>journalctl<span class="w"> </span>-u<span class="w"> </span>codecarbon
</pre></div>
</div>
<p>You are done, CodeCarbon is now running as a service on your machine.</p>
<p>Wait 5 minutes for the first measure to be send to the dashboard at <a class="reference external" href="https://dashboard.codecarbon.io/">https://dashboard.codecarbon.io/</a>.</p>
</section>
<section id="deploy-codecarbon-cli-as-a-service-using-ansible">
<h2>Deploy CodeCarbon CLI as a Service using Ansible<a class="headerlink" href="#deploy-codecarbon-cli-as-a-service-using-ansible" title="Link to this heading"></a></h2>
<p>This section describes how to deploy CodeCarbon as a system service using Ansible automation.</p>
<p>It automate the manual installation done in the previous chapter.</p>
<section id="what-the-playbook-does">
<h3>What the Playbook Does<a class="headerlink" href="#what-the-playbook-does" title="Link to this heading"></a></h3>
<p>The Ansible playbook automates the following tasks:</p>
<ul class="simple">
<li><p>Creates a dedicated system user and group for CodeCarbon</p></li>
<li><p>Sets up a Python virtual environment</p></li>
<li><p>Installs CodeCarbon package</p></li>
<li><p>Configures RAPL permissions for power measurements</p></li>
<li><p>Creates and configures the systemd service</p></li>
<li><p>Sets up the CodeCarbon configuration file</p></li>
<li><p>Starts and enables the service</p></li>
</ul>
</section>
<section id="prerequisites">
<h3>Prerequisites<a class="headerlink" href="#prerequisites" title="Link to this heading"></a></h3>
<ul class="simple">
<li><p>Ansible installed on your machine</p></li>
<li><p>Debian-based target system(s)</p></li>
<li><p>SSH access to target system(s)</p></li>
<li><p>CodeCarbon API credentials from the dashboard</p></li>
</ul>
</section>
<section id="directory-structure">
<h3>Directory Structure<a class="headerlink" href="#directory-structure" title="Link to this heading"></a></h3>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>codecarbon/deploy/ansible/codecarbon_cli_as_a_service/
├── hosts
├── tasks
│   ├── install_codecarbon.yml
│   ├── main.yml
│   ├── rapl.yml
│   └── systemd_service.yml
├── templates
│   ├── codecarbon.config.j2
│   └── systemd_service.j2
└── vars
    └── main.yml
</pre></div>
</div>
</section>
<section id="quick-start">
<h3>Quick Start<a class="headerlink" href="#quick-start" title="Link to this heading"></a></h3>
<ol class="arabic">
<li><p>Set the the target to install in <code class="docutils literal notranslate"><span class="pre">hosts</span></code>:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>yourservername.yourdomain.com   hostname=yourservername ansible_user=root ansible_ssh_private_key_file=~/.ssh/id_ed25519
</pre></div>
</div>
</li>
<li><p>Update the variables in <code class="docutils literal notranslate"><span class="pre">vars/main.yml</span></code> with your configuration:</p>
<div class="highlight-yaml notranslate"><div class="highlight"><pre><span></span><span class="nt">organization_id</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">your_org_id</span>
<span class="nt">project_id</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">your_project_id</span>
<span class="nt">experiment_id</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">your_experiment_id</span>
<span class="nt">api_key</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">your_api_key</span>
</pre></div>
</div>
</li>
<li><p>Run the playbook:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>ansible-playbook<span class="w"> </span>-i<span class="w"> </span>hosts<span class="w"> </span>tasks/main.yml
</pre></div>
</div>
</li>
</ol>
</section>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="comet.html" class="btn btn-neutral float-left" title="Comet Integration" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="output.html" class="btn btn-neutral float-right" title="Output" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2020, BCG GAMMA, Comet.ml, Haverford College, MILA.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>